<template>
	<div id="address">
		<div class="addressEmpty" v-if="show">
			<img src="https://khh-server.oss-cn-beijing.aliyuncs.com/resourse/IMG/static/address_empty.png">
			<p class="test">收货地址在哪里~</p>
			<button class="addAddress" @click="goAdd()">
				添加地址
			</button>
		</div>
		<ul v-else>
			<li class="message" v-for="x in addressList" :id="x.super_id">
				<div class="top" @click="cancel(x.super_id)">
					<p class="names">
						<span class="left">{{x.re_name}}</span>
						<span class="phones">{{x.re_mobile}}</span>
					</p>
					<p class="address"><span v-for="m in x.address_info.value">{{m}}&nbsp;</span>{{x.re_address}}</p>
				</div>
				<div class="edit">
					<div class="delect">
						<div class="Left">
							<img :src="x.isdefault==0?'https://khh-server.oss-cn-beijing.aliyuncs.com/resourse/IMG/static/unselected.png':'https://khh-server.oss-cn-beijing.aliyuncs.com/resourse/IMG/static/checked_b.png'" @click="toggleDefalut($event)">
							<span :class="x.isdefault==0?'notdefault':'default'">默认地址</span>
						</div>
						<div class="right">
							<span class="BJ"><router-link :to="{path:'/addAddress/edit',query:{
							     id: x.super_id,
								 name: x.re_name,
								 mobile:x.re_mobile,
								 address:x.re_address,
								 address_info:x.address_info
							}}">编辑</router-link></span>
							<span class="cut" @click="deletesOpac($event)">删除</span>
						</div>
					</div>
				</div>
			</li>
		</ul>
		<div class="opac" v-if="opaction"></div>
		<div class="textOpac" v-if="text">
			<p>是否要删除此地址</P>
			<div class="sBtn">
				<button @click="goLast" style="border-right:1px solid #e6e6e6;">取消</button>
				<button @click="sure(superid)">确定</button>
			</div>
		</div>
		<v-footer v-if="!show"></v-footer>
		<v-alert v-if="alertshow" :context="context"></v-alert>

	</div>
</template>

<script>
	import Footer from "./footer.vue";
	import Alert from "../alert.vue";
	import config from "../config/config.js";
	import share from "../share/share.js";
    import store from "../store/newStore.js";
    import MtaH5 from 'mta-h5-analysis';
	export default{
		components:{
			"v-footer":Footer,
			"v-alert":Alert
		},
		data(){
			return{
				show: true,
				addressList: [],
				alertshow: false,
				context: "删除成功",
				isDefault:'',
                opaction:false,
                text:false,
				superid:''
			}
		},
        computed:{
            ticket(){
                return this.$store.state.getTicket;
            }
        },
		methods:{
            deletesOpac($event){
                this.opaction = true;
                this.text = true;
                this.superid=$event.currentTarget.parentNode.parentNode.parentNode.parentNode.getAttribute('id');
			},
		    goLast(){
		      this.opaction = false;
		      this.text = false;
			},
			goAdd(){
			    this.$router.push("/addAddress/add")
			},
			toggleDefalut($event){
                var e=$event.currentTarget.parentNode.parentNode.parentNode.parentNode.getAttribute('id');
                var value=JSON.stringify({"super_id":e,"type":2,"token":localStorage.getItem('token')}),
                    key=demo(value);
                axios.post('/Home/Address/UpdateAddress',{
                        value:value,
                        key:key,
					    ticket:this.ticket
                },config)
                    .then(function(response) {
                        if(response.data.code==200){
                            this.$store.commit('increment',response.data.ticket);
                            this.fetchDate();
                            var routeQuery=JSON.parse(localStorage.getItem('routeQuery'));
                            if(localStorage.getItem('chooseAddress')==1){
                                this.$router.push({
                                    path:'/newOrder',
                                    query:{
                                        'iscut':routeQuery.iscut,
                                        'headImg':routeQuery.headImg,
                                        'name':routeQuery.name,
                                        'buy_num':routeQuery.buy_num,
                                        'shopImg':routeQuery.shopImg,
                                        'price_max':routeQuery.price_max,
                                        'price_min':routeQuery.price_min,
                                        'pro_stock':routeQuery.pro_stock,
                                        'title':routeQuery.title,
                                        'spec':routeQuery.spec,
                                        'type':routeQuery.type,
                                        'sku_id':routeQuery.sku_id,
                                        'cutid':routeQuery.cutid,
                                        'delivery_price':routeQuery.delivery_price,
										'cutMoney':routeQuery.cutMoney
                                    }
                                })
                                localStorage.setItem('choosed',super_id);
                                localStorage.removeItem('chooseAddress');
                                localStorage.removeItem('routeQuery');
                            }
						}else{
                            this.$store.commit('increment',response.data.ticket);
						}
                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
			},
            cancel(super_id){
                var that=this;
				var routeQuery=JSON.parse(localStorage.getItem('routeQuery'));
				if(localStorage.getItem('chooseAddress')==1){
				    this.$router.push({
						path:'/newOrder',
						query:{
                            'iscut':routeQuery.iscut,
                            'headImg':routeQuery.headImg,
                            'name':routeQuery.name,
                            'buy_num':routeQuery.buy_num,
                            'shopImg':routeQuery.shopImg,
                            'price_max':routeQuery.price_max,
                            'price_min':routeQuery.price_min,
                            'pro_stock':routeQuery.pro_stock,
                            'title':routeQuery.title,
                            'spec':routeQuery.spec,
                            'type':routeQuery.type,
                            'sku_id':routeQuery.sku_id,
                            'cutid':routeQuery.cutid,
                            'delivery_price':routeQuery.delivery_price,
                            'cutMoney':routeQuery.cutMoney,
                            "isCutZero":that.$route.query.isCutZero
						}
					})
                    localStorage.setItem('choosed',super_id);
                    localStorage.removeItem('chooseAddress');
                  localStorage.removeItem('routeQuery');
				}

			},
            fetchDate(){
                var isAddress=this.$route.params.isAddress;
                if(isAddress==1){
                    this.show=false;
                    var value=JSON.stringify({"type":1,"token":localStorage.getItem('token')}),
                        key=demo(value);
                    axios.post('/Home/Address/GetPointInfo',{
                            value:value,
                            key:key,
						    ticket:this.ticket
                    },config)
                        .then(function(response) {
                            if(response.data.data==''){
                                this.show=true;
                                this.$store.commit('increment',response.data.ticket);
							}else{
                                this.$store.commit('increment',response.data.ticket);
                                this.addressList=response.data.data;
							}


                        }.bind(this))
                        .catch(function (error) {
                            console.log(error);
                        });
                }else{
                    this.show=true
                }
			},
			sure(id){
                var value=JSON.stringify({"super_id":id,"token":localStorage.getItem('token')}),
                    key=demo(value);
                axios.post('/Home/Address/DeleteAddress',{
                    value:value,
                    key:key,
                    ticket:this.ticket
                },config)
                    .then(function(response) {
                        if(response.data.code==200){
                            this.$store.commit('increment',response.data.ticket);
                            this.goLast();
                            this.fetchDate();
                        }else{
                            this.$store.commit('increment',response.data.ticket);
                        }
                    }.bind(this))
                    .catch(function (error) {
                        console.log(error);
                    });
			}
		},
        created() {
            this.fetchDate();
        },
		activated(){
            share({});
            MtaH5.init({
                "sid":'500594815',
                "cid":'500595819',
                "autoReport":0,
                "senseHash":0,
                "senseQuery":0,
                "performanceMonitor":0,
                "ignoreParams":[]
            });
            MtaH5.pgv();
		},
        watch: {
            "$route": "fetchDate"
        }

	}
</script>

<style>
</style>